package org.example.test_03.Service.Impl;

import org.example.test_03.Domian.Ad;
import org.example.test_03.Domian.ReturnData;
import org.example.test_03.Mapper.AdMapper;
import org.example.test_03.Service.AdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author lands
 * @Date 2024/8/14 10:33
 * @Description TODO
 */
@Service
public class AdServeImpl implements AdService {
    @Resource
    AdMapper adMapper;
    @Override
    public ReturnData delete(int id) {
        int result = adMapper.deleteAd(id);
        if (result == 0) {
            return ReturnData.error("删除失败");
        } else {
            return ReturnData.ok("删除成功");
        }
    }

    @Override
    public ReturnData select(String name, String content) {
        return ReturnData.ok(adMapper.selectAd(name,content));
    }

    @Override
    public ReturnData addAd(Ad ad) {
        if(ad.getName().equals("")){
            return ReturnData.error("广告标题不能为空");
        }
        int result = adMapper.addAd(ad);
        if (result == 0) {
            return ReturnData.error("插入失败");
        } else {
            return ReturnData.ok("插入成功");
        }
    }

    @Override
    public ReturnData updateAd(Ad ad) {
        if(ad.getId()==0){
            return ReturnData.error("Id不能为空");
        }
        if(ad.getName().equals("")){
            return ReturnData.error("广告标题不能为空");
        }
        int result = adMapper.updateAd(ad);
        if (result == 0) {
            return ReturnData.error("更新失败");
        } else {
            return ReturnData.ok("更新成功");
        }
    }
}
